/*
 * Copyright 2007-2009 Perttu "celeron55" Ahola <celeron55@gmail.com>
 *
 * This file is part of jukumagic.
 *
 * jukumagic is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * jukumagic is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with jukumagic.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "common.h"
#include "netcomponenttable.h"

NetComponentTable::NetComponentTable(Netlist *netlist)
{
	SetNetlist(netlist);
}

void NetComponentTable::SetNetlist(Netlist *netlist)
{
	m_netlist = netlist;
	
	if(GetView()){
		wxGridTableMessage msg1(this, wxGRIDTABLE_NOTIFY_ROWS_DELETED, 0,
				GetView()->GetNumberRows());
		GetView()->ProcessTableMessage(msg1);
		
		if(m_netlist){
			wxGridTableMessage msg2(this, wxGRIDTABLE_NOTIFY_ROWS_INSERTED, 0,
					m_netlist->m_components.size());
			GetView()->ProcessTableMessage(msg2);
		}
	}
}

int NetComponentTable::GetNumberRows()
{
	if(!m_netlist) return 0;
	return m_netlist->m_components.size();
}

int NetComponentTable::GetNumberCols()
{
	return 4;
}

wxString NetComponentTable::GetValue(int row, int col)
{
	if(!m_netlist) return wxT("invalid netlist");
	if(row >= (int)m_netlist->m_components.size()) return wxT("invalid row");
	if(col==0) return m_netlist->m_components[row]->m_refdes;
	if(col==1) return m_netlist->m_components[row]->m_value;
	if(col==2) return m_netlist->m_components[row]->m_footprint;
	if(col==3) return m_netlist->m_components[row]->m_device;
	return wxT("---");
}

void NetComponentTable::SetValue(int row, int col, const wxString &s)
{
	if(!m_netlist) return;
	if(row >= (int)m_netlist->m_components.size()) return;
	if(col==0) m_netlist->m_components[row]->m_refdes = s;
	if(col==1) m_netlist->m_components[row]->m_value = s;
	if(col==2) m_netlist->m_components[row]->m_footprint = s;
	if(col==3) m_netlist->m_components[row]->m_device = s;
}

bool NetComponentTable::IsEmptyCell(int row, int col)
{
	if(!m_netlist) return wxT("invalid netlist");
	if(row >= (int)m_netlist->m_components.size()) return wxT("invalid row");
	if(col==0) return m_netlist->m_components[row]->m_refdes.IsEmpty();
	if(col==1) return m_netlist->m_components[row]->m_value.IsEmpty();
	if(col==2) return m_netlist->m_components[row]->m_footprint.IsEmpty();
	if(col==3) return m_netlist->m_components[row]->m_device.IsEmpty();
	return true;
}

wxString NetComponentTable::GetColLabelValue(int col)
{
	if(col==0) return wxT("refdes");
	if(col==1) return wxT("value");
	if(col==2) return wxT("footprint");
	if(col==3) return wxT("device");
	return wxT("");
}

